// ==UserScript==
// @name            极简知乎
// @version         0.1.29
// @author          hceasy
// @namespace       https://hceasy.com
// @supportURL      https://github.com/hceasy/simpleZhiHu/issues
// @description     优化阅读界面,免登录,广告去除,黑名单功能.
// @match           *://www.zhihu.com/question/*
// @match           *://www2.zhihu.com/question/*
// @match           *://www.zhihu.com/search*
// @match           *://www.zhihu.com/hot
// @match           *://www.zhihu.com/follow
// @match           *://www.zhihu.com/
// @match           *://zhuanlan.zhihu.com/*
// @match           *://www.zhihu.com/signin*
// @run-at          document-end
// ==/UserScript==
; (function () {
    'use strict'
    // 设置菜单
    const menuHTML = '<div class="extMenu"><img src="https://zhstatic.zhihu.com/assets/error/liukanshan_wire.svg" alt="刘看山" width="15px" height="19px"><p>显示提问标题栏 <input id="showQuestion" type="checkbox"></p><p>浏览器标题替换 <input id="showFakeTitle" type="checkbox"></p><p>页面宽度(694px/80%) <input id="pageWidth" type="text"></p><p>黑名单列表:</p><p><textarea placeholder="刘看山,匿名用户" id="blackList" cols="20" rows="2"></textarea></p><p><button id="saveConfig">保存</button></p></div>'
    const menuCss = '.extMenu {position: fixed;top: 10px;right: 10px;width: 15px;height: 19px;font-size: 12px;overflow: hidden;}.extMenu:hover {width: auto;height: auto;border: 1px solid #000;padding:10px;}.extMenu:hover img {display: none;}'
    const blinkLiu = '.extMenu{animation:jumpLiu 5s infinite}@keyframes jumpLiu{0%{right:10px;background-color:#264653}20%{right:20px;background-color:#2a9d8f}40%{right:30px;background-color:#e9c46a}60%{right:10px;background-color:#f4a261}80%{right:20px;background-color:#e76f51}100%{right:10px;background-color:#264653}}'

    // 区分搜索问答页面
    const pathName = window.location.pathname
    const hostName = window.location.hostname
    let pageType
    if (pathName.indexOf('question') >= 0) {
        pageType = 'question'
    } else if (pathName.indexOf('search') >= 0) {
        pageType = 'search'
    } else if (pathName.indexOf('hot') >= 0 || pathName.indexOf('follow') >= 0 || window.location.href === "https://www.zhihu.com/") {
        pageType = 'hot'
    } else if (pathName.indexOf('signin') >= 0) {
        pageType = 'signin'
    } else if (hostName === "zhuanlan.zhihu.com") {
        pageType = 'zhuanlan'
    }

    
    let link =
        document.querySelector("link[rel*='icon']") ||
        document.createElement('link')
    window.onload = function () {
        const sConfig = window.localStorage
        if (sConfig.fakeTitle === undefined || sConfig.showQuestion === undefined || sConfig.blackList === undefined || sConfig.pageWidth === undefined) {
            sConfig.fakeTitle = 'true'
            sConfig.showQuestion = 'true'
            sConfig.blackList = ''
            sConfig.pageWidth = '694px'
        }
        // 不登陆不让滚动
        let modelsNum = 0
        let fixTimer = setInterval(() => {
            const mainHtml = document.getElementsByTagName('html')[0]
            if (mainHtml.style.overflow === 'hidden') {
                mainHtml.style.overflow = 'auto'
            }
            // 点击无效修复
            // let modals = document.getElementsByClassName('Modal-enter-done')
            // if (modelsNum > 1) {
            //    return
            // }
            // for (let index = 0; index < modals.length; index++) {
            //    let node = modals[index]
            //    if (node.parentNode) {
            //        node.parentNode.removeChild(node);
            //    }
            //    modelsNum ++
            // }
        }, 200);
        // 添加菜单
        let cssFix = document.createElement('style')
        cssFix.innerHTML += menuCss
        if (typeof (sConfig.blinkLiu) === 'undefined') {
            cssFix.innerHTML += blinkLiu
        }
        document.getElementsByTagName('head')[0].appendChild(cssFix)
        let htmlFix = document.createElement('div')
        htmlFix.innerHTML += menuHTML
        document.body.appendChild(htmlFix)

        // 绑定操作
        document.getElementById('showQuestion').checked = JSON.parse(sConfig.showQuestion)
        document.getElementById('blackList').value = sConfig.blackList
        document.getElementById('pageWidth').value = sConfig.pageWidth
        document.getElementById('saveConfig').addEventListener('click', function () {
            sConfig.showQuestion = document.getElementById('showQuestion').checked
            sConfig.blackList = document.getElementById('blackList').value.split(',')
            sConfig.pageWidth = document.getElementById('pageWidth').value
            sConfig.blinkLiu = false
            window.location.reload()
        })

        switch (pageType) {
            case 'question':
                fixQuestionPage()
                fixPageWidth()
                break
            case 'search':
                fixSearchPage()
                break
            case 'hot':
                fixHomePage()
                break
            case 'signin':
                addHotList()
                break
            case 'zhuanlan':
                fixZhuanLan()
                break
        }
    }
    window.onscroll = function () {
        hideAuthor()
    }
    function addHotList () {
        let signButton = document.querySelector('.SignFlow-submitButton')
        if (signButton) {
            let hotButton = signButton.cloneNode(false)
            let parent = signButton.parentNode;
            parent.appendChild(hotButton)
            hotButton.innerHTML = '不想登录,去热榜转转'
            hotButton.onclick = function () {
                location.href = 'https://www.zhihu.com/billboard'
            }
        }
    }
    function fixQuestionPage () {
        const sConfig = window.localStorage
        let cssFix = document.createElement('style')
        // 吸底的评论栏
        cssFix.innerHTML += '.RichContent-actions{bottom:auto !important;}'
        // 直接屏蔽顶部问题相关
        if (sConfig.showQuestion === 'false') {
            cssFix.innerHTML += '.QuestionHeader-footer{display:none !important;}'
            cssFix.innerHTML += '.QuestionHeader{display:none !important;}'
            cssFix.innerHTML += '.Question-main{margin:0 !important;}'
        }
        // 问题页面登录弹窗
        cssFix.innerHTML += '.Modal-backdrop{background-color: transparent;}'
        cssFix.innerHTML += '.signFlowModal{display:none !important;}'
        // 顶部关键词
        cssFix.innerHTML += '.QuestionHeader-tags{display:none !important;}'
        // 问题相关撑满
        // cssFix.innerHTML += '.QuestionHeader-content{width:694px !important;padding:0;}'
        cssFix.innerHTML += '.QuestionHeader-footer{display:none !important;}'
        cssFix.innerHTML += '.QuestionHeader-main {margin:10px;}'
        cssFix.innerHTML += '.QuestionHeader{width:694px;margin:0 auto;padding:0;min-width:auto;}'
        // 未展开时内容居中
        cssFix.innerHTML += '.ListShortcut{margin:0 auto;}'
        // 展开时居中
        cssFix.innerHTML += '.Question-sideColumn{display:none;}'
        cssFix.innerHTML += '.Question-mainColumn{margin:0 auto;}'
        // 内容图片/视频最大300px
        cssFix.innerHTML += '.origin_image{max-width:300px !important;}'
        cssFix.innerHTML += '.RichText-video{max-width:300px !important;}'
        // 内容链接去特征
        cssFix.innerHTML +=
            '.LinkCard{margin:auto !important;display:inline !important;}.LinkCard-content{background-color: transparent;}.LinkCard-title{color:#999 !important}'
        // 点赞
        cssFix.innerHTML +=
            '.VoteButton{color:#999 !important;background: none; !important}'
        // 评论展开宽度
        cssFix.innerHTML += '.Modal--fullPage{width:650px}'
        // 评论展开关闭按钮复位
        cssFix.innerHTML += '.Modal-closeButton{right:0;}'
        cssFix.innerHTML += '.Modal-closeIcon{fill:#919191;}'
        // 广告商品链接
        cssFix.innerHTML +=
            '.RichText-MCNLinkCardContainer{display:none !important;}'
        // 夹缝广告
        cssFix.innerHTML +=
            '.Pc-word{display:none !important;}'
        document.getElementsByTagName('head')[0].appendChild(cssFix)
        // 右侧问题相关
        document.getElementsByClassName('QuestionHeader-side')[1].style.display =
            'none'
        document.getElementsByClassName('Question-sideColumn')[0].style.display =
            'none'
        // 顶部问题标题
        document.getElementsByTagName('header')[0].style.display = 'none'
        // 内容撑满
        document.getElementsByClassName('Question-main')[0].style.width = 'auto'
        document.getElementsByClassName('Question-main')[0].style.padding = '0'
        document.getElementsByClassName('Question-mainColumn')[0].style.margin =
            '0 auto'
    }
    function fixSearchPage () {
        let cssFix = document.createElement('style')
        // header
        cssFix.innerHTML += 'header{display:none !important;}'
        // SearchTabs
        cssFix.innerHTML += '.SearchTabs{display:none !important;}'
        // SearchSideBar
        cssFix.innerHTML += '.SearchSideBar{display:none !important;}'
        // CornerButtons
        cssFix.innerHTML += '.CornerButtons{display:none !important;}'
        // .SearchMain
        cssFix.innerHTML +=
            '.SearchMain{width:100% !important;margin: 0 !important;}'
        // Search-container
        cssFix.innerHTML +=
            '.Search-container{width: auto !important;min-height: auto !important;margin:none !important;}'
        cssFix.innerHTML += '.SearchSections{width:auto !important}'
        // 点赞
        cssFix.innerHTML +=
            '.VoteButton{color:#999 !important;background: none; !important}'
        // 内容图片/视频最大300px
        cssFix.innerHTML += '.origin_image{max-width:300px !important;}'
        cssFix.innerHTML += '.RichText-video{max-width:300px !important;}'
        document.getElementsByTagName('head')[0].appendChild(cssFix)
    }
    function fixZhuanLan () {
        let cssFix = document.createElement('style')
        cssFix.innerHTML += '.Recommendations-Main{display:none !important;}'
        document.getElementsByTagName('head')[0].appendChild(cssFix)
    }
    function fixHomePage () {
        let cssFix = document.createElement('style')
        cssFix.innerHTML += '.GlobalSideBar{display:none !important;}'
        cssFix.innerHTML += '.Topstory-container{width:100% !important;padding:0 !important}'
        cssFix.innerHTML += '.Topstory-mainColumn{width:100% !important;}'
        document.getElementsByTagName('head')[0].appendChild(cssFix)
    }
    function hideAuthor () {
        const answerList = document.getElementsByClassName('List-item')
        for (let index = 0; index < answerList.length; index++) {
            const obj = answerList[index]
            const key = JSON.parse(obj.getElementsByTagName("div")[0].firstChild.getAttribute("data-zop"))
            if (key === null) {
                return
            }
            const blackList = window.localStorage.blackList.split(',')
            blackList.forEach(name => {
                if (key.authorName === name) {
                    obj.style.display = 'none'
                }
            });
        }
    }
    function fixPageWidth(){
        let page = document.querySelector(".Question-mainColumn")
        let header = document.querySelector(".QuestionHeader")
        let headerCont = document.getElementsByClassName("QuestionHeader-content")
        page.style.width = window.localStorage.pageWidth
        header.style.width = window.localStorage.pageWidth
        console.log(headerCont)
        headerCont[1].style.width = '100%'
    }
})()
